
import '../../sass/auth/PermissionManager.scss';
import router from '../../router/Index.js';

export default {
    data(){
        return {
            id:0,
            data: [],
            value: [],
            loading:false,
            name:''
        };
    },
    components:{

    },
    computed: {

    },
    created:function () {
        if(this.$route.query.id) {
            this.id = this.$route.query.id;
            this.name = this.$route.query.name;
            this.type = '绑定';
            this.fetchData();
        }
    },
    methods: {
        handleChange(value, direction, movedKeys) {
            let _this = this;
            if (direction === 'right') {
                _this.bindRole(movedKeys,1);
            }else if (direction === 'left') {
                _this.bindRole(movedKeys,2);
            } else {
                alert('非法操作');
                _this.fetchData();
            }
        },
        async fetchData() {
            let _this=this,response,_response;
            let url = '/backend/role/roleForPermission';
            this.loading = true;
            try {
                response = await fetch(url, {
                    method: 'POST',
                    headers: {
                        'Accept': 'application/json',
                        'Content-type': "application/json",
                    },
                    credentials: 'same-origin',
                    body:JSON.stringify({
                        'id' : _this.id,
                    })
                });
                _response = await response.json();

                if(_response.code===0){
                    _this.data = _response.data.left;
                    _this.value = _response.data.right;
                }else if(_response.code ===-2){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                }else if(_response.code ===-999){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                    setTimeout(function () {
                        window.location.reload();
                    },1500);
                }
                _this.loading = false;
            } catch(err) {
                _this.$message({
                    message: '调用接口失败',
                    type: 'error'
                });
                _this.loading = false;
            }
        },
        async bindRole(movedKeys, type){
            let _this=this,response,_response;
            let url = '/backend/role/bindPermissionRole';
            this.loading = true;
            try {
                response = await fetch(url, {
                    method: 'POST',
                    headers: {
                        'Accept': 'application/json',
                        'Content-type': "application/json",
                    },
                    credentials: 'same-origin',
                    body:JSON.stringify({
                        id:_this.id,
                        type: type,
                        role_ids:movedKeys.join(",")
                    })
                });
                _response = await response.json();

                if(_response.code===0){
                    _this.fetchData();
                }else if(_response.code ===-2){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                }else if(_response.code ===-999){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                    setTimeout(function () {
                        window.location.reload();
                    },1500);
                }
                _this.loading = false;
            } catch(err) {
                _this.$message({
                    message: '调用接口失败',
                    type: 'error'
                });
                _this.loading = false;
            }
        },
        backToPermission(){
            router.push({path:'/auth/permission'});
        },
    }
}